package fy.config.func;

import java.util.List;

import com.seeyon.ctp.organization.bo.MemberPost;
import com.seeyon.ctp.organization.bo.V3xOrgDepartment;
import com.seeyon.ctp.organization.bo.V3xOrgMember;
import com.seeyon.ctp.organization.bo.V3xOrgPost;
import com.seeyon.ctp.organization.manager.OrgManager;
import com.seeyon.ctp.util.annotation.Function;
import com.seeyon.v3x.services.V3XLocator;

import fy.util.MyUtil;

public class FyFunction {
	private static final int CHECK_IS_AUDIT = 0;// 查看人员是否审批人员
	private static final int CHECK_SHENHECISHU = 1;// 非审批人员需要审批几次

	private static int check(long postid, int isAudit) {
		if (postid == 2598272894397269345L) {
			// 审批1
			return 0 + isAudit;
		} else if (postid == 2598242906183847131L) {
			// 审批2
			return 1 + isAudit;
		} else if (postid == 1325558360490659349L) {
			// 审批3
			return 2 + isAudit;
		} else if (postid == 5502099106103904405L) {
			// 审批4
			return 3 + isAudit;
		}
		return -1;
	}

	// 审批层级 需要审批的次数
	@Function(title = "审批次数", description = "需要审批的次数", category = "wf_branch_function")
	public static int auditCount(String memberID) throws Exception {
		OrgManager orgManager = V3XLocator.getInstance().lookup(
				OrgManager.class);
		
		V3xOrgMember member = orgManager.getMemberById(Long.valueOf(memberID));
		long postID = member.getOrgPostId();
		int count = check(postID, CHECK_IS_AUDIT);
		if (count == -1) {
			List<MemberPost> posts = member.getSecondPostByDeptId(member
					.getOrgDepartmentId());
			for (int i = 0, length = posts.size(); i < length; i++) {
				count = check(posts.get(i).getPostId(), CHECK_IS_AUDIT);
				if (count > -1) {
					return count;
				}
			}
		}

		List<V3xOrgPost> deptPosts = orgManager.getDepartmentPost(member
				.getOrgDepartmentId());
		for (int i = 0, length = deptPosts.size(); i < length; i++) {
			int countTemp = check(deptPosts.get(i).getId(), CHECK_SHENHECISHU);
			if (countTemp > -1 && countTemp > count) {
				count = countTemp;
			}
		}
		return count;
	}

	@Function(title = "分管", description = "是否需要分管副总审批", category = "wf_branch_function")
	public static int fenguan(String dept) throws Exception {
		OrgManager orgManager = V3XLocator.getInstance().lookup(
				OrgManager.class);
		long deptID = Long.valueOf(dept);
		int need = fenguan(deptID, orgManager);
		if (need != 1) {
			int count = 0;
			V3xOrgDepartment parentDept = orgManager
					.getParentDepartment(deptID);
			long parentDeptID = 0L;
			while (count < 5 && need == 0 && parentDept != null
					&& (parentDeptID = parentDept.getId()) != 0L
					&& parentDeptID != deptID) {
				deptID = parentDeptID;
				need = fenguan(deptID, orgManager);
				if (need != 1) {
					parentDept = orgManager.getParentDepartment(deptID);
				} else {
					return need;
				}
				count++;
			}
		}
		return need;
	}

	private static int fenguan(long deptID, OrgManager orgManager)
			throws Exception {
		List<V3xOrgPost> deptPosts = orgManager.getDepartmentPost(deptID);
		for (int i = 0, length = deptPosts.size(); i < length; i++) {
			if (deptPosts.get(i).getId() == -803245032428191421L) {// 分管
				return 1;
			}
		}
		return 0;
	}
}
